using MediatR;
using Microsoft.AspNetCore.Mvc;
using WMS.ErrorCode;
using WMS.WebApiWrite.Application.Command;
using WMS.WebApiWrite.DTO;

namespace WMS.WebApiWrite.Controllers
{
    /// <summary>
    /// 库位信息写操作控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StoragelocationController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<StoragelocationController> logger;

        /// <summary>
        /// 构造函数，注入MediatR和日志服务
        /// </summary>
        /// <param name="mediator">MediatR中介者</param>
        /// <param name="logger">日志服务</param>
        public StoragelocationController(IMediator mediator, ILogger<StoragelocationController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 新增库位信息
        /// </summary>
        /// <param name="dto">库位信息数据传输对象</param>
        /// <returns>返回操作结果</returns>
        [HttpPost]
        public async Task<ApiResult<bool>> AddStoragelocation([FromBody] StoragelocationDTO dto)
        {
            logger.LogInformation($"开始新增库位信息，库位名称：{dto.StoragelocationName}");
            
            return await mediator.Send(new AddStoragelocationInfoCommand
            {
                StoragelocationDTO = dto
            });
        }

        /// <summary>
        /// 修改库位信息
        /// </summary>
        /// <param name="dto">库位信息数据传输对象</param>
        /// <returns>返回操作结果</returns>
        [HttpPut]
        public async Task<ApiResult<bool>> UpdateStoragelocation([FromBody] StoragelocationDTO dto)
        {
            logger.LogInformation($"开始修改库位信息，库位编号：{dto.StoragelocationId}");
            
            return await mediator.Send(new UpdateStoragelocationInfoCommand
            {
                StoragelocationDTO = dto
            });
        }

        /// <summary>
        /// 删除库位信息
        /// </summary>
        /// <param name="storagelocationId">库位编号</param>
        /// <param name="operatorName">操作人名称</param>
        /// <returns>返回操作结果</returns>
        [HttpGet]
        public async Task<ApiResult<bool>> DeleteStoragelocation([FromQuery] int storagelocationId, [FromQuery] string operatorName)
        {
            logger.LogInformation($"开始删除库位信息，库位编号：{storagelocationId}");
            
            return await mediator.Send(new DeleteStoragelocationInfoCommand
            {
                StoragelocationId = storagelocationId,
                OperatorName = operatorName
            });
        }
    }
}

